package api;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class ZookeeperConnection {
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        /**
         * {@link ClientCnxn#ClientCnxn(java.lang.String, org.apache.zookeeper.client.HostProvider, int, org.apache.zookeeper.ZooKeeper, org.apache.zookeeper.ClientWatchManager, org.apache.zookeeper.ClientCnxnSocket, long, byte[], boolean)}
         * 启动两条守护线程
         * {@link ClientCnxn.EventThread#EventThread()} 监听事件发生
         * {@link ClientCnxn.SendThread#SendThread(org.apache.zookeeper.ClientCnxnSocket)} 发送心跳给zookeeper
         * */
        ZooKeeper zooKeeper = new ZooKeeper("120.25.24.117:2181", 5000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                countDownLatch.countDown();
                System.out.println("链接成功");
            }
        });
        countDownLatch.await();
        zooKeeper.addAuthInfo("digest","yongjianz:Q4mtAoh1XckntrNUa0Ul6my+7zk=".getBytes());
        byte[] data = zooKeeper.getData("/create/node7", false, new Stat());
        System.out.println(new String(data));
    }
}
